﻿#include "WRWorkTcpCommServerObjRef.h"
#include "WRWorkTcpCommServerInterface.h"
#include "../WRWorkTcpComm.h"

//初始化对象引用。
WRWorkTcpCommServerObjRef::WRWorkTcpCommServerObjRef(int index, WRWorkObject* value, WRWorkTcpCommServerObjRef* parent)
    : m_index(index)
    , m_value(value)
    , m_parent(parent)
{
    //NOTHING
}

//查询指定索引的接口。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryInterface(int index, WRWorkTcpCommServerObject* target)
{
    if (index >= 0 && index < m_iiLen)
    {
        Interface& iiVal = m_iiArr[index];
        return iiVal.create(index, target);
    }
    return 0;
}
//查询指定标识的接口。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryInterface(const QUuid &uuid, WRWorkTcpCommServerObject* target)
{
    for(int i = 0; i < m_iiLen; ++i)
    {
        Interface& iiVal = m_iiArr[i];
        if (iiVal.uuid() == uuid)
        {
            return iiVal.create(i, target);
        }
    }
    WRWorkTcpCommServerInterfaceActivator* activator = WRWorkTcpComm::selectServerInterface(uuid);
    if (activator)
    {
        void* reference = m_value->queryWorkDealer(uuid);
        if (!reference) reference = m_value->queryTaskDealer(uuid);
        if (!reference) reference = m_value->queryDataSource(uuid);
        if (!reference) reference = m_value->queryEventSource(uuid);
        if (!reference) reference = m_value->queryStateSource(uuid);
        if (reference)
        {
            Interface iiVal(uuid, reference, activator);
            int iiIdx = m_iiLen;
            m_iiArr[iiIdx] = iiVal;
            ++m_iiLen;
            return iiVal.create(iiIdx, target);
        }
    }
    return 0;
}

//查询指定标识的工作处理者。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryWorkDealer(const QUuid &uuid, WRWorkTcpCommServerObject* target)
{
    for(int i = 0; i < m_iiLen; ++i)
    {
        Interface& iiVal = m_iiArr[i];
        if (iiVal.uuid() == uuid)
        {
            return iiVal.create(i, target);
        }
    }
    void* reference = m_value->queryWorkDealer(uuid);
    if (reference)
    {
        WRWorkTcpCommServerInterfaceActivator* activator = WRWorkTcpComm::selectServerInterface(uuid);
        if (activator)
        {
            Interface iiVal(uuid, reference, activator);
            int iiIdx = m_iiLen;
            m_iiArr[iiIdx] = iiVal;
            ++m_iiLen;
            return iiVal.create(iiIdx, target);
        }
    }
    return 0;
}
//查询指定标识的任务处理者。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryTaskDealer(const QUuid& uuid, WRWorkTcpCommServerObject* target)
{
    for(int i = 0; i < m_iiLen; ++i)
    {
        Interface& iiVal = m_iiArr[i];
        if (iiVal.uuid() == uuid)
        {
            return iiVal.create(i, target);
        }
    }
    void* reference = m_value->queryTaskDealer(uuid);
    if (reference)
    {
        WRWorkTcpCommServerInterfaceActivator* activator = WRWorkTcpComm::selectServerInterface(uuid);
        if (activator)
        {
            Interface iiVal(uuid, reference, activator);
            int iiIdx = m_iiLen;
            m_iiArr[iiIdx] = iiVal;
            ++m_iiLen;
            return iiVal.create(iiIdx, target);
        }
    }
    return 0;
}
//查询指定标识的数据源接口。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryDataSource(const QUuid& uuid, WRWorkTcpCommServerObject* target)
{
    for(int i = 0; i < m_iiLen; ++i)
    {
        Interface& iiVal = m_iiArr[i];
        if (iiVal.uuid() == uuid)
        {
            return iiVal.create(i, target);
        }
    }
    void* reference = m_value->queryDataSource(uuid);
    if (reference)
    {
        WRWorkTcpCommServerInterfaceActivator* activator = WRWorkTcpComm::selectServerInterface(uuid);
        if (activator)
        {
            Interface iiVal(uuid, reference, activator);
            int iiIdx = m_iiLen;
            m_iiArr[iiIdx] = iiVal;
            ++m_iiLen;
            return iiVal.create(iiIdx, target);
        }
    }
    return 0;
}
//查询指定标识的事件源接口。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryEventSource(const QUuid& uuid, WRWorkTcpCommServerObject* target)
{
    for(int i = 0; i < m_iiLen; ++i)
    {
        Interface& iiVal = m_iiArr[i];
        if (iiVal.uuid() == uuid)
        {
            return iiVal.create(i, target);
        }
    }
    void* reference = m_value->queryEventSource(uuid);
    if (reference)
    {
        WRWorkTcpCommServerInterfaceActivator* activator = WRWorkTcpComm::selectServerInterface(uuid);
        if (activator)
        {
            Interface iiVal(uuid, reference, activator);
            int iiIdx = m_iiLen;
            m_iiArr[iiIdx] = iiVal;
            ++m_iiLen;
            return iiVal.create(iiIdx, target);
        }
    }
    return 0;
}
//查询指示标识的状态源接口。
WRWorkTcpCommServerInterface* WRWorkTcpCommServerObjRef::queryStateSource(const QUuid& uuid, WRWorkTcpCommServerObject* target)
{
    for(int i = 0; i < m_iiLen; ++i)
    {
        Interface& iiVal = m_iiArr[i];
        if (iiVal.uuid() == uuid)
        {
            return iiVal.create(i, target);
        }
    }
    void* reference = m_value->queryStateSource(uuid);
    if (reference)
    {
        WRWorkTcpCommServerInterfaceActivator* activator = WRWorkTcpComm::selectServerInterface(uuid);
        if (activator)
        {
            Interface iiVal(uuid, reference, activator);
            int iiIdx = m_iiLen;
            m_iiArr[iiIdx] = iiVal;
            ++m_iiLen;
            return iiVal.create(iiIdx, target);
        }
    }
    return 0;
}
